/*
 * Simple.cpp
 *
 *  Created on: Aug 22, 2011
 *      Author: erudhes
 */
#include <wx/dcbuffer.h>
#include "MainPanel.h"
#include "MainGameController.h"

BEGIN_EVENT_TABLE(MainPanel, wxPanel)
EVT_MOTION(MainPanel::onMouseMotion)
EVT_LEFT_DOWN(MainPanel::onMouseDown)
EVT_LEFT_UP(MainPanel::onMouseUp)
EVT_PAINT(MainPanel::onPaint)
END_EVENT_TABLE()

MainPanel::MainPanel(wxFrame* parent): 	wxPanel(parent), mGameController(new MainGameController(this))
{
	this->SetBackgroundStyle(wxBG_STYLE_CUSTOM);
//	/*wxLogWindow* log = */new wxLogWindow(NULL, wxT("Log"));
}

/*virtual*/
MainPanel::~MainPanel()
{
	// Nothing to do yet
}

void MainPanel::onMouseDown(wxMouseEvent& mouseEvent)
{
	mGameController->handleMouseDown(mouseEvent.GetPosition());
}

void MainPanel::onMouseUp(wxMouseEvent& mouseEvent)
{
	mGameController->handleMouseUp(mouseEvent.GetPosition());
}

void MainPanel::onMouseMotion(wxMouseEvent& mouseEvent)
{
	wxFrame* parent = (wxFrame*)this->GetParent();
    wxString title;
    wxPoint position = mouseEvent.GetPosition();

    // Update the title of the window
    title.Printf(wxT("%s [%d, %d]"), wxT("Main Frame"), position.x, position.y);
    parent->SetTitle(title);
    mGameController->handleMouseMotion(mouseEvent.GetPosition());
}

void MainPanel::onPaint(wxPaintEvent& paintEvent)
{
	wxAutoBufferedPaintDC dc(this);

	dc.DrawBitmap(mGameController->getBitmapToPaint(), 0, 0, true);
}

bool MainPanel::hostNew(const wxString& level)
{
	return mGameController->host(level);
}

bool MainPanel::joinNew(const wxString& host, int port)
{
	return mGameController->join(host, port);
}

void MainPanel::configurationChange()
{
	mGameController->configurationChange();
}

void MainPanel::kickOffClient()
{
	mGameController->kickOffClient();
}

void MainPanel::finish()
{
	mGameController->finish(IGameController::USER_EXIT);
}

void MainPanel::restart()
{
	mGameController->finish(IGameController::RESTART_FROM_USER);
}
